#include "stm32f10x.h"                  // Device header
#include "OLED.h"
#include "Key.h"
#include <stdlib.h>
#include <math.h>
#include "Delay.h"

//函数声明

	
//场景人物结构体，用于碰撞检测
struct Object_Position{
	uint8_t minX,minY,maxX,maxY;
};


//显示游戏分数
int Score;
void Show_Score(void)
{
	OLED_ShowNum(98,0,Score,5,OLED_6X8);
}


//显示场景地面
uint16_t Ground_Pos;
void Show_Ground(void)
{
	if(Ground_Pos<128)
	{
			for(uint8_t i=0;i<128;i++)
		{
			OLED_DisplayBuf[7][i]=Image_Ground[i+Ground_Pos];
		}
	}
	else
	{
			for(uint8_t i=0;i<255-Ground_Pos;i++)
		{
			OLED_DisplayBuf[7][i]=Image_Ground[i+Ground_Pos];
		}
			for(uint8_t i=255-Ground_Pos;i<128;i++)
		{
			OLED_DisplayBuf[7][i]=Image_Ground[i-(255-Ground_Pos)];
		}
	}
	
}

//显示障碍物
uint8_t barrier_flag;
uint8_t Barrier_Pos;

struct Object_Position barrier;

void Show_Barrier(void)
{
	if(Barrier_Pos>=143)
	{
		barrier_flag=rand()%3;
	}
	
	OLED_ShowImage(127-Barrier_Pos,44,16,18,Image_Barrier[barrier_flag]);
	
	barrier.minX=127-Barrier_Pos;
	barrier.maxX=143-Barrier_Pos;
	barrier.minY=44;
	barrier.maxY=62;
}


//显示云朵
uint8_t Cloud_Pos;
void Show_Cloud(void)
{
	OLED_ShowImage(127-Cloud_Pos,9,16,8,Image_Cloud);
}

uint8_t dino_jump_flag=0;
extern uint8_t KeyNum;
uint16_t jump_t;
uint8_t Jump_Pos;
extern float Pi;

struct Object_Position dino;

//显示小恐龙
void Show_Dino(void)
{
	
	KeyNum=Key_GetNum();
	if(KeyNum==1)dino_jump_flag=1;
	Jump_Pos=28*sin((float)(Pi*jump_t/1000));
	
	if(dino_jump_flag==0)
	{
		if(Cloud_Pos%2==0)OLED_ShowImage(0,44,16,18,Image_Dino[0]);
		else OLED_ShowImage(0,44,16,18,Image_Dino[1]);
	}
	
	else
	{
		OLED_ShowImage(0,44-Jump_Pos,16,18,Image_Dino[2]);
	}
	
	dino.minX=0;
	dino.maxX=16;
	dino.minY=44-Jump_Pos;
	dino.maxY=62-Jump_Pos;
}


//检测碰撞
int isColliding(struct Object_Position *a,struct Object_Position *b)
{
	if((a->maxX>b->minX)&&(a->minX<b->maxX)&&(a->maxY>b->minY)&&(a->minY<b->maxY))
	{
		OLED_Clear();
		OLED_ShowString(28,24,"Game Over",OLED_8X16);
		OLED_Update();
		Delay_s(1);
		OLED_Clear();
		OLED_Update();
		return 1;
	}
	return 0;
}


//整合动画
int DinoGame_Animation(void)
{
	while(1)
	{
		int return_flag;
		OLED_Clear();
		Show_Score();
		Show_Ground();
		Show_Barrier();
		Show_Cloud();
		Show_Dino();
		OLED_Update();
		return_flag=isColliding(&dino,&barrier);
		if(return_flag==1)
		{
			return 0;
		}
	}
	
}


//中断
void Dino_Tick(void)
{
	static uint8_t Score_Count,Ground_Count,Cloud_Count;
	Score_Count++;
	Ground_Count++;
	Cloud_Count++;

	if(Score_Count>=100)
	{
		Score_Count=0;
		Score++;
	}
	
	if(Ground_Count>=20)
	{
		Ground_Count=0;
		Ground_Pos++;
		Barrier_Pos++;
		if(Ground_Pos>=256)Ground_Pos=0;
		if(Barrier_Pos>=144)Barrier_Pos=0;
	}
	
	if(Cloud_Count>=50)
	{
		Cloud_Count=0;
		Cloud_Pos++;
		if(Cloud_Pos>=200)Cloud_Pos=0;
	}
	
	if(dino_jump_flag==1)
	{
		jump_t++;
		if(jump_t>=1000)
		{
			jump_t=0;
			dino_jump_flag=0;
		}
	}

}


//分数重置
void DinoGame_Pos_Init(void)
{
	Score=Ground_Pos=Barrier_Pos=Cloud_Pos=Jump_Pos=0;
}

//游戏界面
void Show_Game_UI(void)
{
	OLED_ShowImage(0,0,16,16,Image_Return);
	OLED_ShowString(0,16,"谷歌小恐龙",OLED_8X16);
}

//游戏选项选择
uint8_t game_flag=1;
int Game(void)
{
	while(1)
	{
		KeyNum=Key_GetNum();
		uint8_t game_flag_temp=0;
		if(KeyNum==1)//上一项
		{
			game_flag--;
			if(game_flag<=0)game_flag=2;
		}
		else if(KeyNum==2)//下一项
		{
			game_flag++;
			if(game_flag>=3)game_flag=1;
		}
		else if(KeyNum==3)//确认
		{
			OLED_Clear();
			OLED_Update();
			game_flag_temp=game_flag;
		}
		
		if(game_flag_temp==1){return 0;}
		else if(game_flag_temp==2){DinoGame_Pos_Init();DinoGame_Animation();}
		
		switch(game_flag)
		{
			case 1:
				Show_Game_UI();
				OLED_ReverseArea(0,0,16,16);
				OLED_Update();
				break;
			
			case 2:
				Show_Game_UI();
				OLED_ReverseArea(0,16,80,16);
				OLED_Update();
				break;
			
		
		}
	}
}
